package com.abu.kpaymodel.aliconfig;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "alipay")     //通过setter方法将其连接配置
public class AliPayTemplate {

    private String serverUrl = null;
    private String appId = null;
    private String privateKey = null;
    private String alipayPublicKey = null;
    private String signType = null;
    private String returnUrl = null;
    private String notifyUrl = null;


    public String alipay(PayParam payParam) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl,
                appId,
                privateKey,
                "json",
                "UTF-8",
                alipayPublicKey,
                signType);  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //创建API对应的request
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl); //在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\"" + payParam.getOut_trade_no() + "\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":" + payParam.getTotal_amount() + "," +
                "    \"subject\":\"" + payParam.getSubject() + "\"," +
                "    \"body\":\"" + payParam.getBody() + "\"," +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                "    \"extend_params\":{" +
                "    \"sys_service_provider_id\":\"2088511833207846\"" +
                "    }" +
                "  }"); //填充业务参数
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }
}
